//
//  IntegralGiveViewController.m
//  Logistics
//
//  Created by luojinrong11 on 2018/4/20.
//  Copyright © 2018年 com.Logistics. All rights reserved.
//

#import "IntegralGiveViewController.h"
#import "GRPurchaseSuccessViewController.h"
#import "GRRegisterViewController.h"
#import "XGPWManager.h"
#import "GRPurchaseSuccessViewController.h"
@interface IntegralGiveViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;

@end
@implementation IntegralGiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"龟米赠送";
    self.topOffset.constant = IS_IPHONEX ? 107:83;
    self.minePointLabel.text = [NSString stringWithFormat:@"当前龟米%@",self.tortoiseRice];
    [self.pointTextField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    
}

-(void)textChange:(UITextField *)textField{
    if (textField.text.length >0) {
        textField.textAlignment =  NSTextAlignmentLeft;
        textField.font = [UIFont boldSystemFontOfSize:45];
        if ([textField.text floatValue] >[self.tortoiseRice floatValue] ||[textField.text floatValue]<1) {
            if ([textField.text floatValue] >[self.tortoiseRice floatValue]) {
                self.minePointLabel.text = @"超出当前龟米";
                 self.minePointLabel.textColor = HEXCOLOR(0xE5004F);
            }
            self.submitBt.backgroundColor = HEXCOLOR(0x999999);
            self.submitBt.userInteractionEnabled = NO;
        }else{
         self.minePointLabel.text = [NSString stringWithFormat:@"当前龟米%@",self.tortoiseRice];
         self.minePointLabel.textColor = HEXCOLOR(0x888888);
            self.submitBt.backgroundColor = HEXCOLOR(0xFBD62D);
            self.submitBt.userInteractionEnabled = YES;
        }
    }else {
        textField.textAlignment =  NSTextAlignmentCenter;
        textField.font = [UIFont systemFontOfSize:14];
        self.submitBt.backgroundColor = HEXCOLOR(0x999999);
        self.submitBt.userInteractionEnabled = NO;
    }
    
}
- (IBAction)submitAction:(id)sender {
WEAKSELF
    
    if(_isPW){
    XGPasswordViewStyle * style = [[XGPasswordViewStyle alloc]init];
    style.title = @"请输入安全密码";
    style.middleContext = [NSString stringWithFormat:@"当前龟米"];
    style.amountLeftText = @"龟米";
    style.amountText = self.pointTextField.text;

    [XGPWManager showImportPasswordInView:[UIApplication sharedApplication].keyWindow PWViewStyle:style FinishBack:^(NSString *password) {
        NSDictionary * dic = @{
                               @"safePassword":password,
                               @"mobile":self.mobileNumTF.text,
                               @"change":self.pointTextField.text
                               };
        [GRPersonAction IntegralGive:dic completionHandler:^(BOOL success, id responseObject, GRError *error) {
            if (success) {
                NSLog(@"赠送成功");
                GRPurchaseSuccessViewController  * successVC = [[GRPurchaseSuccessViewController  alloc]init];
                successVC.successType = SuccessTypeGiveRice;
                [self.navigationController pushViewController:successVC animated:YES];
            }else{
                if ([error.errorCode integerValue] == 1004) {
                    [[LFAlertBox defaultAlert ]showAlertTitle:nil  content:@{@"text":@"密码错误",@"textColor":HEXCOLOR(0x444444) ,@"font":     [UIFont boldSystemFontOfSize:18],@"backgroundColor":HEXCOLOR(0xffffff)}  cancleBtn:@{@"text":@"忘记密码",@"textColor":HEXCOLOR(0x444444) ,@"font": [UIFont systemFontOfSize:14] ,@"backgroundColor":HEXCOLOR(0xffffff)} sureBtn:@{@"text":@"重试",@"textColor":HEXCOLOR(0x444444) ,@"font": [UIFont systemFontOfSize:14] ,@"backgroundColor":HEXCOLOR(0xffffff)}  completion:^(NSInteger index) {
                        if (index == 1) {
                            [weakSelf submitAction:nil];
                        }else{
                            GRRegisterViewController * forgetPassWordVC = [[GRRegisterViewController alloc]init];
                          
                            forgetPassWordVC.registerMode = GRRegisterModeForgetTradePassword;
                            [weakSelf.navigationController pushViewController:forgetPassWordVC animated:YES];
                        }
                    }];
                }else{
                    [GRHUD showErrorMsg:error.errorMsg];
                 
                }
            }
        }];
    } forgetWordBlock:^{
        GRRegisterViewController * forgetPassWordVC = [[GRRegisterViewController alloc]init];
        
        forgetPassWordVC.registerMode = GRRegisterModeForgetTradePassword;
        [weakSelf.navigationController pushViewController:forgetPassWordVC animated:YES];
        
    }];
    }else {
        [[LFAlertBox defaultAlert ]showAlertTitle:  @{@"text":@"温馨提示",@"textColor":HEXCOLOR(0x444444) ,@"font": [UIFont boldSystemFontOfSize:18] }  content:@{@"text":@"为确保账户资金安全，请设置安全密码不设置，龟米兑换，提现将受限制",@"textColor":HEXCOLOR(0x888888) ,@"font": [UIFont systemFontOfSize:14] }  cancleBtn:@{@"text":@"暂不设置",@"textColor":HEXCOLOR(0x444444) ,@"font": [UIFont systemFontOfSize:14] }   sureBtn:@{@"text":@"去设置",@"textColor":HEXCOLOR(0x444444) ,@"font": [UIFont systemFontOfSize:14] }  completion:^(NSInteger index){
            if (index == 1) {
                GRRegisterViewController * forgetPassWordVC = [[GRRegisterViewController alloc]init];
                forgetPassWordVC.finishBlock = ^{
                    _isPW = YES;
                };
                forgetPassWordVC.registerMode = GRRegisterModeSetTradePassword;
                [weakSelf.navigationController pushViewController:forgetPassWordVC animated:YES];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
